package cn.wolfcode.shop.web.util;

import org.springframework.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class MobileValidator implements ConstraintValidator<IsMobile,String> {
    private boolean required;
    @Override
    public void initialize(IsMobile constraintAnnotation) {
        this.required = constraintAnnotation.required();
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if(required){
            //这个参数必须要验证
            return ValidatorUtil.isMobile(s);
        }else{
            //如果required为false表示为空时可以不校验  不为空必须要校验
            if(StringUtils.isEmpty(s)){
                return true;
            }
            return ValidatorUtil.isMobile(s);
        }
    }
}
